/*
 * Copyright (c) 2005 -2008 Reliable Source, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1,
 * BSD, and Eclipse Public License v1.0 which accompanies this
 * distribution and is available at:
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 * 		http://www.opensource.org/licenses/bsd-license.php
 * 		http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *     IBM Corporation	     -  http://ibm.com
 *     	- initial API and implementation
 *
 *     patientos.org         - http://patientos.org
 *     	- domain and database persistence layer
 *
 *     Reliable Source, Inc. - http://reliablesource.org
 *     	- Symbiosys Application Framework,
 *     	- Symbiosys Application, Symbiosys Product
 *
 */
package org.reliablesource.symbiosys.ui.tests.viewers;

import junit.framework.TestCase;

import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.util.ILogger;
import org.eclipse.jface.util.ISafeRunnableRunner;
import org.eclipse.jface.util.Policy;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


/**
 * 
 * ViewerTestCase.java<br>
 * Created on: Jun 27, 2008<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
abstract public class ViewerTestCase extends TestCase {
    Display fDisplay;
    protected Shell fShell;
    protected Viewer fViewer;

    public ViewerTestCase(String name) {
        super(name);
    }

    protected void assertSelectionEquals(String message,
	    ISelection _expected) {
        ISelection _selection_ = fViewer.getSelection();
        assertTrue(_selection_ instanceof IStructuredSelection);

        assertEquals("selectionEquals - " + message, _selection_,
            _expected);
    }

    public void interact() {
        Shell shell = fShell;

        if ((shell != null) && !shell.isDisposed()) {
            Display display = shell.getDisplay();

            if (display != null) {
                while (shell.isVisible())
                    display.readAndDispatch();
            }
        }
    }

    protected void openBrowser() {
        fDisplay = Display.getCurrent();

        if (fDisplay == null) {
            fDisplay = new Display();
        }

        fShell = new Shell(fDisplay);
        fShell.setSize(500, 500);
        fShell.setLayout(new FillLayout());
        
        fShell.open();

        // processEvents();
    }

    public void processEvents() {
        Shell shell = fShell;

        if ((shell != null) && !shell.isDisposed()) {
            Display display = shell.getDisplay();

            if (display != null) {
                while (display.readAndDispatch()) {
                    // loop until there are no more events to dispatch
                }
            }
        }
    }

    public void setUp() {
        Policy.setLog(new ILogger() {
                public void log(IStatus status) {
                    fail(status.getMessage());
                }
            });
        SafeRunnable.setRunner(new ISafeRunnableRunner() {
                public void run(ISafeRunnable code) {
                    try {
                        code.run();
                    } catch (Throwable th) {
                        throw new RuntimeException(th);
                    }
                }
            });
        openBrowser();
    }



    void sleep(int d) {
        processEvents();

        try {
            Thread.sleep(d * 1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    public void tearDown() {
        processEvents();
        fViewer = null;

        if (fShell != null) {
            fShell.dispose();
            fShell = null;
        }
    }
}
